package com.example.patients.database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.patients.entity.PatientsBean;

import java.util.ArrayList;
import java.util.List;
//数据库创建和初始化
public class DbPatients extends SQLiteOpenHelper {
    private static SQLiteDatabase db;
    public DbPatients(@Nullable Context context) {
        super(context, "sickness.db", null, 1);
        System.out.println("数据库创建成功");
        db = getReadableDatabase();
    }
    //通过姓名和病床号获取列表
    public static List<PatientsBean> getPatientListByNumberAndName(String number , String name) {
        List<PatientsBean> list = new ArrayList<>();
        String sql = "select * from student where number like '%" + number + "%' and name like '%" + name + "%'";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String number1 = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name1 = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String year = cursor.getString(cursor.getColumnIndex("year"));
            @SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
            @SuppressLint("Range") String sickness = cursor.getString(cursor.getColumnIndex("sickness"));
            @SuppressLint("Range") String taboo = cursor.getString(cursor.getColumnIndex("taboo"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            PatientsBean patientsBean = new PatientsBean(id,number1, name1,year, sex,sickness,taboo,phone);
            list.add(patientsBean);
        }
        return list;
    }
    //通过病床号获取列表
    public static List<PatientsBean> getPatientListByNumber(String number) {
        List<PatientsBean> list = new ArrayList<>();
        String sql = "select * from student where number="+number;
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String number1 = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String year = cursor.getString(cursor.getColumnIndex("year"));
            @SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
            @SuppressLint("Range") String sickness = cursor.getString(cursor.getColumnIndex("sickness"));
            @SuppressLint("Range") String taboo = cursor.getString(cursor.getColumnIndex("taboo"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            PatientsBean patientsBean = new PatientsBean(id,number1, name,year, sex,sickness,taboo,phone);
            list.add(patientsBean);
        }

        return list;
    }
    //通过姓名获取列表
    public static List<PatientsBean> getPatientListByName(String name) {
        List<PatientsBean> list = new ArrayList<>();
        String sql = "SELECT * FROM student WHERE name LIKE '%" + name + "%'";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name1 = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String year = cursor.getString(cursor.getColumnIndex("year"));
            @SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
            @SuppressLint("Range") String sickness = cursor.getString(cursor.getColumnIndex("sickness"));
            @SuppressLint("Range") String taboo = cursor.getString(cursor.getColumnIndex("taboo"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            PatientsBean patientsBean = new PatientsBean(id, number, name1, year, sex, sickness, taboo, phone);
            list.add(patientsBean);
        }
        return list;
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = ("CREATE TABLE student(id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "number VARCHAR(50) not null," +
                "name VARCHAR(20) not null," +
                "year VARCHAR(2) not null," +
                "sex VARCHAR(2) not null," +
                "sickness varchar(60) not null," +
                "taboo Varchar(60) not null," +
                "phone VARCHAR(11) not null," +
                "total INTEGER not null DEFAULT 0)");

        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists student");
        onCreate(db);
    }
    //添加病人
    public void insertPatients(String number, String name, String year, String sex, String sickness,
                               String taboo, String phone) {
        db.execSQL("INSERT INTO student(number,name,year,sex,sickness,taboo,phone)VALUES(?,?,?,?,?,?,?)",
                new String[]{number,name,year,sex, sickness,taboo,phone});
    }
    /**
     * 浏览
     * */
    public static List<PatientsBean> search() {
        List<PatientsBean> list = new ArrayList<>();
        String sql = "select * from student order by number";
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String year = cursor.getString(cursor.getColumnIndex("year"));
            @SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
            @SuppressLint("Range") String sickness = cursor.getString(cursor.getColumnIndex("sickness"));
            @SuppressLint("Range") String taboo = cursor.getString(cursor.getColumnIndex("taboo"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            PatientsBean patientsBean = new PatientsBean(id,number, name,year, sex,sickness,taboo,phone);
            list.add(patientsBean);
        }
        return list;
    }
    //删除
    public static int deleteItemFromPatientByNumber(String number) {
        int i = db.delete("student", "number=?", new String[]{number + ""});
        return i;
    }
    /**
     * 查询病床号是否存在
     * */
    public static PatientsBean QueryFromPatientByNumber(String number) {
        PatientsBean patientsBean = null;
        String sql = "select * from student where number ="+number;
        Cursor cursor = db.rawQuery(sql, null);
        while (cursor.moveToNext()) {
            @SuppressLint("Range") int id = cursor.getInt(cursor.getColumnIndex("id"));
            @SuppressLint("Range") String number1 = cursor.getString(cursor.getColumnIndex("number"));
            @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
            @SuppressLint("Range") String year = cursor.getString(cursor.getColumnIndex("year"));
            @SuppressLint("Range") String sex = cursor.getString(cursor.getColumnIndex("sex"));
            @SuppressLint("Range") String sickness = cursor.getString(cursor.getColumnIndex("sickness"));
            @SuppressLint("Range") String taboo = cursor.getString(cursor.getColumnIndex("taboo"));
            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
            patientsBean = new PatientsBean(id, number1, name, year, sex, sickness, taboo, phone);
        }
        return patientsBean;
    }

    //修改数据
    public boolean updatePatient(String number, String name, String year, String sex, String sickness, String taboo, String phone){
        ContentValues contentValues=new ContentValues();
        contentValues.put("name",name);
        contentValues.put("year",year);
        contentValues.put("sex",sex);
        contentValues.put("sickness",sickness);
        contentValues.put("taboo",taboo);
        contentValues.put("phone",phone);
        String sql="number"+"=?";
        String[] strings=new String[]{number};
        return
                db.update("student",contentValues,sql,strings)>0;
    }

    //更新总人数
    public void updateTotalPatientCount(int total) {
        ContentValues contentValues=new ContentValues();
        contentValues.put("total",total);
//        db.close();
    }

    // 查询总人数
    public int getTotalPatientCount() {
        int total = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM student", null);
        if (cursor.moveToFirst()) {
            total = cursor.getInt(0);
        }
        cursor.close();
//        db.close(); // 关闭数据库连接
        return total;
    }
}
